package snmp;

import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.TransportMapping;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.*;
import org.snmp4j.transport.DefaultUdpTransportMapping;

import java.io.IOException;
import java.net.SocketException;
import java.util.Vector;

/**
 * </p>
 *
 * @author zhangsh
 * @version 1.0.0
 * @since 2019-05-18
 */
public class GetOID {

    public static void main(String[] args) {

        // 设置target
        CommunityTarget target = new CommunityTarget();
        Address localAddress = GenericAddress.parse("udp:localhost/161");

        target.setAddress(localAddress);
        target.setCommunity(new OctetString("public"));
        target.setRetries(2);
        target.setTimeout(5*1000L);
        target.setVersion(SnmpConstants.version2c);

        //
        TransportMapping transportMapping = null;
        try {
            transportMapping = new DefaultUdpTransportMapping();
            transportMapping.listen();

            // 设置发送器，以及配置使用的协议
            Snmp snmp = new Snmp(transportMapping);

            // request
            PDU request = new PDU();
            String[] oids = {"1.3.6.1.2.1.25.2.3.1.2",  //type 存储单元类型
                    "1.3.6.1.2.1.25.2.3.1.3",  //descr
                    "1.3.6.1.2.1.25.2.3.1.4",  //unit 存储单元大小
                    "1.3.6.1.2.1.25.2.3.1.5",  //size 总存储单元数
                    "1.3.6.1.2.1.25.2.3.1.6"}; //used 使用存储单元数;
            for(int i=0; i<oids.length; i++){
                request.add(new VariableBinding(new OID(oids[i])));
            }
//            request.add(new VariableBinding(new OID("1.3.6.1.2.1.1.1")));
//            request.add(new VariableBinding(new OID("1.3.6.1.2.1.25.3.3.1.2")));
//            request.add(new VariableBinding(new OID("1.3.6.1.2.1.25.2.3.1.5")));
//            request.add(new VariableBinding(new OID("1.3.6.1.2.1.25.2.3.1.2")));
            request.setType(PDU.GETNEXT);

            ResponseEvent responseEvent = snmp.send(request, target);
            PDU response = responseEvent.getResponse();

            Vector<? extends VariableBinding> variableBindings = responseEvent.getResponse().getVariableBindings();

            /*for(VariableBinding binding : variableBindings){
                System.out.println(binding.toValueString());
            }*/


            VariableBinding resultBind = null;
            for(int i=0; i<response.size(); i++){
                resultBind = response.get(i);

                System.out.println(response.getRequestID().toString() + ":  " + response.get(i).toValueString());
            }

        } catch (SocketException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                transportMapping.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
